package com.suke.czx.common.utils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

/**
 * Redis工具类
 *
 * @author czx
 * @email object_czx@163.com
 * @date 2017-07-17 21:12
 */
@Component
public class RedisUtils {
	private org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    /**
	 * 从缓存获取数据
	 * @param key
	 * @return
	 */
	public <T> T get(String key) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		Object value=valueOper.get(key);
		if(value!=null) {
			logger.debug("Got an object,key: '{}'",key);
		} else {
			logger.debug("Got null,key: '{}'",key);
		}
		return (T)valueOper.get(key);
	}
	
	/**
	 * 从缓存获取数据
	 * @param key
	 * @return
	 */
	public <T> List<T> get(Collection<String> keys) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		return (List<T>)valueOper.multiGet(keys);
	}
	
	
	/**
	 * 设置数据到缓存
	 * @param key
	 * @param value
	 */
	public void set(String key,Object value) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		valueOper.set(key, value);
	}
	
	/**
	 * 设置数据到缓存
	 * @param key
	 * @param value
	 */
	public void set(Map<String, Object> map) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		valueOper.multiSet(map);
	}
	
	/**
	 * 设置数据到缓存
	 * @param key
	 * @param value
	 * @param timeout	缓存时间,单位:秒
	 */
	public void set(String key,Object value,long timeout) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		valueOper.set(key, value, timeout, TimeUnit.SECONDS);
	}
	
	/**
	 * 自增操作
	 * @param key
	 * @param delta		累加值
	 * @return
	 */
	public long increment(String key,long delta) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		return valueOper.increment(key, delta);
	}
	
	/**
	 * 自增操作
	 * @param key
	 * @param delta		累加值
	 * @param timeout	超时时间,单位:秒
	 * @return
	 */
	public long increment(String key,long delta,long timeout) {
		ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
		long num=valueOper.increment(key, delta);
		if(num==1) {
			expire(key, timeout);
		}
		return num;
	}
	
	/**
	 * 设置超时时间,单位:秒
	 * @param key
	 * @param second
	 */
	public void expire(String key,long second) {
		redisTemplate.expire(key, second, TimeUnit.SECONDS);
	}
	
	public <T> T hash(String key,String hashkey) {
		HashOperations<String, String, Object> hashOper=redisTemplate.opsForHash();
		return (T)hashOper.get(key,hashkey);
	}
	
	public void hash(String key,String hashkey,Object value) {
		HashOperations<String, String, Object> hashOper=redisTemplate.opsForHash();
		hashOper.put(key, hashkey, value);
	}
	
	public void hashdel(String key,String hashkey) {
		HashOperations<String, String, Object> hashOper=redisTemplate.opsForHash();
		hashOper.delete(key, hashkey);
	}
	
	public <T> Set<T> hashkeys(String key) {
		HashOperations<String, String, Object> hashOper=redisTemplate.opsForHash();
		return (Set<T>)hashOper.keys(key);
	}
	
	public <T> List<T> hashvals(String key) {
		HashOperations<String, String, Object> hashOper=redisTemplate.opsForHash();
		return (List<T>)hashOper.values(key);
	}
	
	public boolean hashIfAbsent(String key,String hashkey,Object value) {
		HashOperations<String, String, Object> hashOper=redisTemplate.opsForHash();
		return hashOper.putIfAbsent(key, hashkey, value);
	}
	
	public void del(String key) {
		redisTemplate.delete(key);
	}
}
